﻿using netDxf;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Tables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestDxfDocument

{

    class ISYC_306_02
    {
        //图F.60  ISYC 306-02-可转位刀片阶梯钻
        public static void ISYC_306_02_j(Dictionary<string, Object> data)  //可转位刀片阶梯钻
        {
            DxfDocument dxf = new DxfDocument();
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);
            double sl_ = Convert.ToDouble(data["lpr"]);
            double scale = Convert.ToDouble(data["scale"]);
            //  double sl_ = 45;
            //  double scale = 1;
            if (sl_ <= 67)
            {
                scale = 4;
            }
            else if (67 < sl_ && sl_ <= 75)
            {
                scale = 2.5;
            }
            else if (75 < sl_ && sl_ <= 100)
            {
                scale = 2;
            }
            else
            {
                scale = 1;
            }
            /*
            double dc_1 = 10*scale ;
            double dc_2 = 17 * scale;
            double dc_3 = 22 * scale;
            double dconms = 19 * scale;
            double sd_2 = 12 * scale;
            //double sdl_1 = 12 * scale;
            double sdl_2 = 15 * scale;
            double sd_3 = 30 * scale;
            double lu = 36 * scale;
            double lcf = 38 * scale;
            double lpr = 46 *scale;
            double ls = 16 * scale;
            double sta_1 = 160 ;
            double sta_2 = 90;
            double sta_3 = 45;
            */
            double dc_1 = Convert.ToDouble(data["dc_1"]) * scale;
            double dc_2 = Convert.ToDouble(data["dc_2"]) * scale;
            double dc_3 = Convert.ToDouble(data["dc_3"]) * scale;
            double dconms = Convert.ToDouble(data["dconms"]) * scale;
            double sd_2 = Convert.ToDouble(data["sd_2"]) * scale;
            //double sdl_1 = 12 * scale;
            double sdl_2 = Convert.ToDouble(data["sdl_2"]) * scale;
            double sd_3 = Convert.ToDouble(data["sd_3"]) * scale;
            double lu = Convert.ToDouble(data["lu"]) * scale;
            double lcf = Convert.ToDouble(data["lcf"]) * scale;
            double lpr = Convert.ToDouble(data["lpr"]) * scale;
            double ls = Convert.ToDouble(data["ls"]) * scale;
            double sta_1 = Convert.ToDouble(data["sta_1"]);
            double sta_2 = Convert.ToDouble(data["sta_2"]);
            double sta_3 = Convert.ToDouble(data["sta_3"]);
            
            double sta_1_ = sta_1 * pi / 180;
            double sta_2_ = sta_2 * pi / 180;
            double sta_3_ = sta_3 * pi / 180;

            double oal = ls + lpr;
            double pl = (dc_1 / 2.0) / (Math.Tan(sta_1_ / 2));
            //切削部分
            Vector2 p0 = new Vector2(0, 0);
            Vector2 p0_1 = fun.Polar(p0, pi, 5);
            Vector2 p0_2 = fun.Polar(p0, 0, oal + 5);
            Vector2 p0_3 = fun.Polar(p0, 0, pl);
            Vector2 p1 = fun.Polar(p0_3, pi / 2, (dc_1 / 2.0) / 5.0);
            Vector2 p2 = new Vector2(0, ((dc_1 / 2.0) - p1.Y) / 2.0 + p1.Y);
            Vector2 p3 = fun.Polar(p2, fun.CalAngle(p2, p1) + 150 * pi / 180, fun.CalDistance(p2, p1));
            Vector2 p4 = fun.Polar(p3, fun.CalAngle(p3, p2) + 90 * pi / 180, fun.CalDistance(p2, p1));
            Vector2 p5 = fun.Polar(p4, fun.CalAngle(p4, p3) + 150 * pi / 180, fun.CalDistance(p2, p1));
            Vector2 p6 = fun.Polar(p5, fun.CalAngle(p5, p4) + 90 * pi / 180, fun.CalDistance(p2, p1));
            Vector2 p7 = fun.Polar(p0_3, pi / -2, dc_1 / 4.0);
            Vector2 p8_1 = fun.Polar(p7, pi / -2 + 10 * pi / 180, oal);
            Vector2 p8_2 = fun.Polar(p0_3, pi / -2, (dc_1 / 2.0) * (4 / 5.0));
            Vector2 p8_3 = fun.Polar(p8_2, 0, oal);
            Vector2 p8 = fun.Inters(p7, p8_1, p8_2, p8_3);
            Vector2 p9 = new Vector2(sd_2, -dc_2 / 2.0);
            Vector2 p10_1 = fun.Polar(p9, sta_2_, oal);
            Vector2 p10_2 = fun.Polar(p8, 0, oal);
            Vector2 p10 = fun.Inters(p8, p10_2, p9, p10_1);
            Vector2 p11 = fun.Polar(p9, 0, dc_2 * (43 / 171.0));
            Vector2 p12 = fun.mirrorpoint(p9, p0_1, p0_2);
            Vector2 p14 = fun.mirrorpoint(p11, p0_1, p0_2);
            Vector2 p15 = fun.Polar(p12, -sta_2_, fun.CalDistance(p12, p14));
            Vector2 p16 = fun.Polar(p15, fun.CalAngle(p15, p12) - pi / 2, fun.CalDistance(p15, p12));
            Vector2 p17 = fun.Polar(p12, fun.CalAngle(p12, p14) - pi / 4, fun.CalDistance(p12, p14) / 4.5);
            Vector2 p18 = fun.Polar(p15, fun.CalAngle(p15, p12) - pi / 4, fun.CalDistance(p15, p12) / 4.5);
            Vector2 p19 = fun.Polar(p18, fun.CalAngle(p18, p17) - pi / 2, fun.CalDistance(p18, p17));
            Vector2 p20 = fun.Polar(p19, fun.CalAngle(p19, p18) - pi / 2, fun.CalDistance(p19, p18));
            Vector2 p21 = fun.Polar(p3, fun.CalAngle(p3, p4), fun.CalDistance(p3, p4) / 2.0);
            Vector2 p13_0 = fun.Polar(p21, 0, oal);
            Vector2 p13 = fun.Inters(p21, p13_0, p12, p15);
            Vector2 p22 = fun.Polar(p14, pi / -2, dc_2 / 50.0);
            Vector2 p23 = fun.Polar(p0_3, pi / -2, fun.CalDistance(p0, p7) / 3.0);
            Vector2 p24_1 = fun.Polar(p1, fun.CalAngle(p1, p6), fun.CalDistance(p1, p6) / 2.0);
            Vector2 p24_2 = fun.Polar(p24_1, 0, oal);
            Vector2 p24 = fun.Inters(p24_1, p24_2, p23, fun.Polar(p23, 15 * pi / 180, oal));
            Vector2 p25_1 = fun.Polar(p18, pi / -2, oal);
            Vector2 p25 = fun.Inters(p18, p25_1, p9, fun.Polar(p9, 20 * pi / 180, oal));
            Vector2 p26_1 = fun.Polar(p24, 0, oal);
            Vector2 p26_2 = fun.Polar(p25, pi / 2, oal);
            Vector2 p26 = fun.Inters(p24, p26_1, p25, p26_2);
            Vector2 p27_1 = fun.Polar(p11, pi / 2, oal);
            Vector2 p27_2 = fun.Polar(p25, 0, oal);
            Vector2 p27 = fun.Inters(p11, p27_1, p25, p27_2);
            Vector2 p28_0 = fun.Polar(p10, 0, oal);
            Vector2 p28 = fun.Inters(p26, p25, p10, p28_0);
            Vector2 p29 = new Vector2(sd_3, dc_3 / 2.0);
            Vector2 p30 = fun.Polar(p29, pi + sta_3_, fun.CalDistance(p12, p14));
            Vector2 p31 = fun.Polar(p30, fun.CalAngle(p30, p29) - pi / 2, fun.CalDistance(p29, p30));
            Vector2 p32 = fun.Polar(p31, fun.CalAngle(p31, p30) - pi / 2, fun.CalDistance(p31, p30));
            Vector2 p33 = fun.Polar(p30, fun.CalAngle(p30, p29) - pi / 4, fun.CalDistance(p12, p17));
            Vector2 p34 = fun.Polar(p31, fun.CalAngle(p31, p30) - pi / 4, fun.CalDistance(p30, p33));
            Vector2 p35 = fun.Polar(p34, fun.CalAngle(p34, p33) - pi / 2, fun.CalDistance(p33, p34));
            Vector2 p36 = fun.Polar(p35, fun.CalAngle(p35, p34) - pi / 2, fun.CalDistance(p35, p34));

            Vector2 p37 = fun.mirrorpoint(p30, p0_1, p0_2);
            Vector2 p38 = fun.mirrorpoint(p29, p0_1, p0_2);
            Vector2 p39 = new Vector2(lcf, dc_3 / 2.0 - dc_3 * (7 / 470.0));

            Vector2 p40_0 = fun.Polar(p39, pi + pi / 4, oal);
            Vector2 p40 = fun.Inters(p39, p40_0, p26, p26_1);
            Vector2 p39_1 = fun.Polar(p39, pi, oal);
            Vector2 p39_2 = fun.Polar(p40, pi / 2, oal);
            Vector2 p39_3 = fun.Inters(p40, p39_2, p39, p39_1);
            Arc arc1 = fun.DrawArc(p40, p39_3, p39);
            Vector2 p41_1 = fun.Polar(p39, pi, oal);
            Vector2 p41 = fun.Inters(p29, p32, p39, p41_1);
            Vector2 p42_0 = fun.Polar(p27, 0, oal);
            Vector2 p42 = fun.Inters(p27, p42_0, p38, p37);
            Vector2 p43 = fun.Polar(p38, sta_3_ + pi / 4, p29.Y - p41.Y);
            Vector2 p44_0 = fun.Polar(p43, pi / 2 + pi / 4, oal);
            Vector2 p44 = fun.Inters(p27, p42_0, p43, p44_0);
            Vector2 p45_0 = fun.Polar(p44, pi / 2, oal);
            Vector2 p45 = fun.Inters(p26, p40, p44, p45_0);
            Vector2 p46_0 = fun.Polar(p43, pi / 2 + 0.001 * pi / 180, oal);
            Vector2 p46 = fun.Inters1(p43, p46_0, arc1, 1);
            Vector2 p47 = new Vector2((lpr - lcf) / 2.0 + lcf, dc_3 / 2.0);
            Vector2 p47_1 = fun.Polar(p39, 0, oal);
            Vector2 p47_2 = fun.Polar(p47, pi / -2, oal);
            Vector2 p47_3 = fun.Inters(p47, p47_2, p39, p47_1);
            Vector2 p48 = fun.Polar(p47_3, pi, fun.CalDistance(p47, p47_3));
            Vector2 p49_1 = fun.Polar(p38, pi / -2, -(p38.Y - p43.Y));
            Vector2 p49_2 = fun.Polar(p49_1, 0, oal);
            Vector2 p49_3 = fun.Polar(p47, pi / -2, oal);
            Vector2 p49 = fun.Inters(p47, p49_3, p49_1, p49_2);
            Vector2 p50_0 = fun.Polar(p49, pi / 2, -(p49.Y - p43.Y));
            Vector2 p50 = fun.Polar(p50_0, pi, fun.CalDistance(p49, p50_0));
            Vector2 p51 = new Vector2(lpr, dc_3 / 2.0);
            Vector2 p52 = fun.Polar(p49, 0, fun.CalDistance(p47, p51));
            //非切削部分
            Vector2 p53 = fun.Polar(p0, 0, ls + lpr);
            Vector2 p54 = fun.Polar(p53, pi / 2, dconms / 2.0 - (dconms * (24.5 / 367.73)) / Math.Sqrt(2));
            Vector2 p55 = fun.Polar(p54, pi / 2 + pi / 4, dconms * (24.5 / 367.73));
            Vector2 p56 = fun.Polar(p55, pi, ls - (ls * (16.7 / 367.4)) - (dconms * (24.5 / 367.73)));
            Vector2 p57 = fun.mirrorpoint(p54, p0_1, p0_2);
            Vector2 p58 = fun.mirrorpoint(p55, p0_1, p0_2);
            Vector2 p59 = fun.mirrorpoint(p56, p0_1, p0_2);
            Vector2 p60 = fun.Polar(p56, pi / -2, (ls * (16.7 / 367.4)));
            Vector2 p61_0 = fun.Polar(p60, pi, oal);
            Vector2 p61 = fun.Inters(p60, p61_0, p51, p52);
            Vector2 p62 = fun.mirrorpoint(p60, p0_1, p0_2);
            Vector2 p63 = fun.mirrorpoint(p61, p0_1, p0_2);
            var polyline1 = Pus.AddPointsToLwPolyLine(p11, p9, p10, p8, p7, p1, p2, p3, p21, p13, p21, p4, p5, p6, p1);
            var polyline2 = Pus.AddPointsToLwPolyLine(p12, p15, p16, p22, p30, p29, p32, p31, p30, p33, p36, p35, p34, p33, p30, p22, p14, p12, p17, p18, p19, p20, p17);
            var polyline3 = Pus.AddPointsToLwPolyLine(p11, p27, p25, p28, p10, p28, p26, p40, p39, p41, p39, p40, p24, p23);
            var polyline4 = Pus.AddPointsToLwPolyLine(p27, p42, p38, p43, p46, p43, p44, p43, p50, p49, p47, p48, p39, p40, p45, p44, p45, p44, p42);
            var polyline5 = Pus.AddPointsToLwPolyLine(p47, p51, p52, p49, p52, p63, p62, p60, p61, p60, p56, p55, p54, p57, p58, p55, p58, p59, p62);
            polyline3.Vertexes[7].Bulge = Math.Tan((pi / 2) / 4);
            polyline3.Vertexes[10].Bulge = -Math.Tan((pi / 2) / 4);
            polyline4.Vertexes[8].Bulge = -Math.Tan((pi / 2) / 4);
            polyline4.Vertexes[10].Bulge = -Math.Tan((pi / 2) / 4);
            polyline4.Vertexes[12].Bulge = -Math.Tan((pi / 2) / 4);
            //dxf.AddEntity(new EntityObject[] { polyline1, polyline2, polyline3 ,polyline4  ,polyline5});
            fun.PolyFill(dxf, polyline1, 0.5, contour);
            fun.PolyFill(dxf, polyline2, 0.5, contour);
            fun.PolyFill(dxf, polyline3, 0.5, contour);
            fun.PolyFill(dxf, polyline4, 0.5, contour);
            fun.PolyFill(dxf, polyline5, 0.5, contour);
            //标注
            Vector2 v1 = fun.Polar(p50, pi, p49.X - p50.X);
            Vector2 v2 = fun.mirrorpoint(p2, p0_1, p0_2);
            Vector2 v3 = fun.mirrorpoint(p3, p0_1, p0_2);
            Vector2 v4 = fun.Inters(p0_1, p0_2, p45, p43);
            Vector2 v5 = fun.Polar(v4, 0, 0.1);
            Vector2 v6 = fun.Polar(p56, 0, ls / 6.0);
            Vector2 v7 = fun.Polar(p0, pi + 70 * pi / 180, dc_1 * 3);
            Vector2 v8 = fun.Inters(p51, p52, p0_1, p0_2);
            Vector2 v9 = fun.Polar(p47, pi, fun.CalDistance(p48, p39));
            Vector2 v10 = fun.Polar(p0, pi / -4, 1.2 * 0.5 * scale);
            Vector2 v11 = fun.Polar(v10, pi / -4, dconms / 2.0 * 0.8);
            Vector2 v12 = fun.Polar(p49, pi, fun.CalDistance(p48, p39));
            Vector2 v13 = fun.Polar(v8, pi / -4, 1.7 * 0.5 * scale);
            Vector2 v14 = fun.Polar(v13, pi / -4, dconms);
            //fun.dimrot1(dxf, p3, p51, 0, dc_1 * 2 - (p51.Y - p3.Y) / 2.0, 2, 1.2 * scale, 1.6 * scale, 0.9, "LTA,LF", "", "");
            fun.dimrot1(dxf, p12, p3, 0, dc_1 * 1.5 - (p12.Y - p3.Y) / 2.0, 2, 1.2 * scale, 1.6 * scale, 0.9, "SDL1", "", "");
            fun.dimrot1(dxf, p2, p3, 0, dc_1 * 2 - (p3.Y - p2.Y) / 2.0, 2, 1.2 * scale, 1.6 * scale, 0.9, "PL", "", "");
            fun.dimrot1(dxf, p8, p3, 90, dc_1, 2, 1.2 * scale, 1.6 * scale, 0.9, "DC1", "", "");
            //fun.dimrot1(dxf, p12, p9, 90, dc_1 * 1.5 + (p9.X), 2, 1.2 * scale, 1.6 * scale, 0.9, "DC_2", "", "");
            fun.dimrot1(dxf, p29, p38, 90, dc_1 * 2 + (p38.X), 2, 1.2 * scale, 1.6 * scale, 0.9, "DCn", "", "");
            fun.dimrot1(dxf, p55, p58, -90, dc_1, 2, 1.2 * scale, 1.6 * scale, 0.9, "DCONMS", "", "");
            //fun.dimrot1(dxf, p9, p0, 180, dc_1 * 1.5 - (p9.Y) / 2.0, 2, 1.2 * scale, 1.6 * scale, 0.9, "SD_2", "", "");
            //fun.dimrot1(dxf, p9, p44, 180, dc_1 * 1.5 - (p9.Y - p44.Y) / 2.0, 2, 1.2 * scale, 1.6 * scale, 0.9, "SDL_2", "", "");
            fun.dimrot1(dxf, p38, p0, 180, dc_1 * 2 - (p38.Y) / 2.0, 2, 1.2 * scale, 1.6 * scale, 0.9, "SDn", "", "");
            fun.dimrot1(dxf, v1, p0, 180, dc_1 * 2.5 - (v1.Y) / 2.0, 2, 1.2 * scale, 1.6 * scale, 0.9, "LU", "", "");
            fun.dimrot1(dxf, p50, p0, 180, dc_1 * 3 - (p50.Y) / 2.0, 2, 1.2 * scale, 1.6 * scale, 0.9, "LCF", "", "");
            fun.dimrot1(dxf, p52, p0, 180, dc_1 * 3.5 - (p52.Y) / 2.0, 2, 1.2 * scale, 1.6 * scale, 0.9, "LPR", "", "");
            fun.dimrot1(dxf, p57, p52, 180, dc_1 * 3.5 - (p52.Y - p57.Y) / 2.0, 2, 1.2 * scale, 1.6 * scale, 0.9, "LS", "", "");
            fun.dimrot1(dxf, p57, p0, 180, dc_1 * 4 - p57.Y / 2.0, 2, 1.2 * scale, 1.6 * scale, 0.9, "OAL", "", "");
            fun.jdbz2(dxf, new Line(v3, v2), new Line(p3, p2), dc_1 * 2.5, 1.2 * scale, 1.6 * scale, 0.9, "SIG", "", "");
            //fun.jdbz2(dxf, new Line(p9, fun.Polar(p9, pi / -2, 0.1)), new Line(p27, p37), dc_1, 1.2 * scale, 1.6 * scale, 0.9, "STA_2", "", "");
            fun.jdbz2(dxf, new Line(p44, p43), new Line(v4, v5), dc_1 * 1.7, 1.2 * scale, 1.6 * scale, 0.9, "STAn", "", "");
           /* fun.Singlelinearrow(dxf, "CCMS", v6, 60, fun.Polar(v6, 60 * pi / 180, dconms / 2.0), fun.Polar(v6, 60 * pi / 180, dconms), 1.2 * scale, 1.6 * scale);
            fun.Singlelinearrow(dxf, "FDP", v7, 0, fun.Polar(v7, 180 * pi / 180, dconms / 2.0), fun.Polar(v7, 180 * pi / 180, dconms), 1.2 * scale, 1.6 * scale);
            fun.Singlelinearrow(dxf, "DF", v9, 90, fun.Polar(v9, 90 * pi / 180, dconms / 4.0), fun.Polar(v9, 90 * pi / 180, dconms / 2.0), 1.2 * scale, 1.6 * scale);
            fun.Singlelinearrow(dxf, "", v12, -90, fun.Polar(v12, -90 * pi / 180, dconms / 4.0), fun.Polar(v12, -90 * pi / 180, dconms / 2.0), 1.2 * scale, 1.6 * scale);

            fun.Axis(dxf, fun.Polar(p53, -pi / 4, dc_1 * 3), 0, -90, "Z", "X", "Y", 0.5 * scale, 1.2 * scale, 1);
            fun.Axis(dxf, p0, 0, -90, "", "", "", 0.5 * scale, 1.2 * scale, 1);
            fun.Axis(dxf, v8, 0, -90, "", "", "", 0.5 * scale, 1.2 * scale, 1);*/
            Line linev1 = new Line(v10, v11);
            Line linev2 = new Line(p47, v9);
            Line linev3 = new Line(p49, v12);
            Line linev4 = new Line(v13, v14);
            Line linev5 = new Line(p0_1, p0_2);
            linev5.Lineweight = Lineweight.W25;//Lineweight指线宽
            linev5.Layer = centre;//Layer指图层
            linev5.LinetypeScale = 5 / scale * 1.5;//指线性比例
            linev5.Linetype = Linetype.DashDot;//Linetype指是点划线还是虚线什么的

            Text txt1 = new Text("CIP", fun.Polar(v11, pi / -2, 1.2 * scale), 1.6 * scale, 0);
            Text txt2 = new Text("PCS,MCS", fun.Polar(v14, pi / -2, 1.2 * scale), 1.6 * scale, 0);
            /*dxf.AddEntity(new EntityObject[] { linev1 ,linev2,linev3,linev4,linev5
            ,txt1 ,txt2
            });*/

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }


            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }


    }
}


